package com.example.suspensionbardemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.example.suspensionbardemo.adapter.ViewPager2Adapter
import com.example.suspensionbardemo.databinding.ActivityRefreshBinding
import com.example.suspensionbardemo.fragment.RecyclerViewFragment
import com.google.android.material.tabs.TabLayoutMediator

class RefreshViewPager2Activity : AppCompatActivity() {
    val labels = arrayOf("个性推荐", "歌单", "主播电台", "排行榜")
    lateinit var binding: ActivityRefreshBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityRefreshBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.titleBar.tvTitle.setText("ViewPager2")
        initListener();

        val pagerAdapter =
            ViewPager2Adapter(
                this,
                getPageFragments()
            );
        binding.viewpager.setAdapter(pagerAdapter);

        TabLayoutMediator(binding.tablayout,  binding.viewpager) { tab, position ->
            tab.text = labels[position]
        }.attach()

        binding.viewpager.beginFakeDrag()

    }

    private fun initListener() {
        binding.refreshLayout.setOnRefreshListener {
            println("RefreshActivity.initListener")
            binding.refreshLayout.finishRefresh();

        }
    }

    private fun getPageFragments(): List<Fragment>? {
        val data: MutableList<Fragment> = ArrayList()
        data.add(RecyclerViewFragment())
        data.add(RecyclerViewFragment())
        data.add(RecyclerViewFragment())
        data.add(RecyclerViewFragment())
        return data
    }

}